package com.cpr.ams.web.controller;

import com.cpr.ams.bean.BasePrivilege;
import com.cpr.ams.exception.AmsException;
import com.cpr.ams.service.IBasePrivilegeService;
import com.cpr.ams.util.Result;
import com.cpr.ams.util.StatusCode;
import com.cpr.ams.vm.PrivilegeTree;
import com.sun.istack.internal.NotNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 权限管理
 * @Author: GX Cui
 * @Date 3:41 下午 2021/4/12
 */
@Api(value = "权限管理",tags = {"权限管理接口"})
@Validated
@RestController
@RequestMapping("/privilege")
public class PrivilegeController {

    @Autowired
    private IBasePrivilegeService basePrivilegeService;

    @ApiOperation(value = "查询所有")
    @GetMapping(value = "findAll")
    public Result findAll(){
        List<BasePrivilege> list = basePrivilegeService.findAll();
        return Result.success(list);
    }



    @ApiOperation(value = "通过parentId查询")
    @GetMapping(value = "findByParentId")
    public Result findByParentId(Integer id){
        List<BasePrivilege> list = basePrivilegeService.findByParentId(id);
        return Result.success(list);
    }

    @ApiOperation(value ="保存或更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="主键",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name="name",value="权限名称",paramType = "query",required = true),
            @ApiImplicitParam(name="description",value="权限描述",paramType = "query",required = false),
            @ApiImplicitParam(name="route",value="权限路由",paramType = "query",required = true),
            @ApiImplicitParam(name="type",value="权限类型(菜单或者方法)",paramType = "query",required = true),
            @ApiImplicitParam(name="icon",value="权限图标",paramType = "query",required = false),
            @ApiImplicitParam(name="parentId",value="父权限id",paramType = "query",required = false),
    })
    @PostMapping(value = "saveOrUpdate")
    public Result saveOrUpdate(Integer id,
                               @NotNull String name,
                               String description,
                               @NotNull String route,
                               @NotNull String type,
                               String icon,
                               Integer parentId){
        BasePrivilege basePrivilege = new BasePrivilege();
        basePrivilege.setId(id);
        basePrivilege.setName(name);
        basePrivilege.setDescription(description);
        basePrivilege.setRoute(route);
        basePrivilege.setType(type);
        basePrivilege.setIcon(icon);
        basePrivilege.setParentId(parentId);
        int i = basePrivilegeService.saveOrUpdate(basePrivilege);
        return Result.success(i);
    }

    @ApiOperation(value = "查询树")
    @GetMapping(value = "findPrivilegeTree")
    public Result findPrivilegeTree(){
        List<PrivilegeTree> list = basePrivilegeService.findPrivilegeTree();
        return Result.success(list);
    }

    @ApiOperation(value = "通过id删除")
    @GetMapping(value = "deleteById/{id}")
    public Result deleteById(@PathVariable int id){
        try{
            basePrivilegeService.deleteById(id);
            return Result.success("删除成功");
        }catch (AmsException ams){
            return Result.fail(StatusCode.PRIVILEGE_NOT_EXIST);
        }

    }

}
